<template>
    <div class="default-main ba-table-box">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <div style="background: #fff; padding: 13px 15px;">
            <span :class="[state.table == 'local' ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeTable('local')">{{ t('本地') }}</span>
            <span :class="[state.table == 'qcc' ? 'btn btn-info ml-12' : 'btn ml-12']" @click="changeTable('qcc')">{{ t('企查查') }}</span>
        </div>

        <!-- 表格顶部菜单 -->
        <!-- 自定义按钮请使用插槽，甚至公共搜索也可以使用具名插槽渲染，参见文档 -->
        <TableHeader :buttons="['add', 'columnDisplay']" :quick-search-placeholder="t('搜索')">
            <template #quickSearchPrepend>
                <span style="">
                    

                    <el-form :inline="true" class="demo-form-inline">
                        <el-cascader
                            :options="search.industry.old"
                            :props="search.selectCategory"
                            :collapse-tags="true"
                            :collapse-tags-tooltip="true"
                            clearable
                            :placeholder="t('所属行业')"
                            :show-all-levels="false"
                            v-model="search.form.category"
                        />
                        <el-input v-model="search.form.keyword" :placeholder="t('关键词')" clearable style="margin-left: 8px;" />
                    </el-form>
                    
                </span>
            </template>
            <template #btnBefore>
                <span class="btn ml-12" style="" @click="clickSearch()">查询</span>
            </template>
        </TableHeader>

        <!-- 表格 -->
        <!-- 表格列有多种自定义渲染方式，比如自定义组件、具名插槽等，参见文档 -->
        <!-- 要使用 el-table 组件原有的属性，直接加在 Table 标签上即可 -->
        
        <Table ref="tableRef">
            
            <template #status>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('操作')" align="center" width="150" class-name="business-name" height="200">
                    <template #default="scope">
                        <div v-if="scope.row.user_id > 0">
                            <div>
                                <div class="btn ml-0 mt-0" @click="add(scope)">{{ t('添加') }}</div>
                            </div>
                            
                            <div :class="[scope.row.status.includes('1') ? 'btn btn-info ml-0 mt-5' : 'btn ml-0 mt-5']" @click="setStatus(scope.row.id, 1, scope.row)">{{ t('成交') }}</div>
                            <div :class="[scope.row.status.includes('0') ? 'btn btn-info mt-5 ml-5' : 'btn mt-5 ml-5']" @click="setStatus(scope.row.id, 0, scope.row)">{{ t('终止') }}</div>
                            <div class="btn ml-0 mt-5" @click="edit(scope)">{{ t('编辑') }}</div>
                            <el-popconfirm title="确定要删除这条数据吗?" @confirm="del(scope)">
                                <template #reference>
                                    <div class="btn ml-5 mt-5">{{ t('删除') }}</div>
                                </template>
                            </el-popconfirm>
                        </div>
                        <div v-if="scope.row.user_id == 0">
                            <div class="btn ml-0 mt-0" @click="add(scope)">{{ t('添加') }}</div>
                            <div v-if="scope.row.user_id == 0" :class="[scope.row.status.includes('1') ? 'btn btn-info mt-0 ml-5' : 'btn mt-0 ml-5']" @click="setStatus(scope.row.id, 1, scope.row)">{{ t('完成') }}</div>
                            <div class="btn ml-0 mt-5" @click="edit(scope)">{{ t('编辑') }}</div>
                            <el-popconfirm title="确定要删除这条数据吗?" @confirm="del(scope)">
                                <template #reference>
                                    <div class="btn ml-5 mt-5">{{ t('删除') }}</div>
                                </template>
                            </el-popconfirm>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #files>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('附加文件')" align="center" min-width="120">
                    <template #default="scope">
                        <div style="height: 120px; overflow: auto;">
                            <el-link :href="item.url" target="_blank" class="tag" style="margin-top: 5px;" v-for="(item, index) in scope.row.files" :key="index">{{ item.name }}</el-link>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #create_time>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('时间')" align="center" width="100">
                    <template #default="scope">
                        <div style="">
                            <div>{{ scope.row.create_date_text }}</div>
                            <div>{{ scope.row.create_time_text }}</div>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #category>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('所属分类')" align="center" width="220">
                    <template #default="scope">
                        <div style="">
                            <div v-if="scope.row.category && scope.row.category.parentCategory">{{ scope.row.category.parentCategory.name }}</div>
                            <div v-if="scope.row.category">{{ scope.row.category.name }}</div>
                        </div>
                    </template>
                </el-table-column>
            </template>
            <template #corp_name_cn>
                <!-- 在插槽内，您可以随意发挥，通常使用 el-table-column 组件 -->
                <el-table-column :label="t('公司名')" align="center">
                    <template #default="scope">
                        <!-- 自定义 -->
                        <a class="corporate-name" target="_blank" style="color: #000; text-decoration: none;" :href="'https://www.qcc.com/web/search?key=' + scope.row.corp_name_cn + '&searchIndex=%7B%22name%22%3A%22' + scope.row.corp_name_cn + '%22%7D'">{{ scope.row.corp_name_cn }}</a>
                    </template>
                </el-table-column>
            </template>
        </Table>

        <!-- 表单 -->
        <PopupForm />

        <!-- <Quotation v-if="quotation.id != 0" :data="quotation" @closeViewQuotation="closeViewQuotation" /> -->

    </div>
</template>

<script setup lang="ts">
import { ref, provide, onMounted, reactive } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Quotation from './quotation/index.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'

import { getCategoryOldApi } from '/@/api/most/corp/supplier'



defineOptions({
    name: 'corp/supplier/index',
})

const { t } = useI18n()
const tableRef = ref()




const clickSearch = () => {
    console.log(search.form);
    baTable.table.filter!.form = search.form;
    baTable.getIndex()
}



const search = reactive({
    industry: {
        'old': []
    },
    selectCategory: {
        expandTrigger: 'hover',
        multiple: true,
        clearable: true,
        label: 'name',
        value: 'id'
    },
    form: {
        category: '',
        keyword: '',
    }
})

const state = reactive({
    table: 'local',
    showImportFrom: false,
    loading: false,
    type: '',
    industry: {
        'old': []
    },
    pop: false
});

const quotation = reactive({
    id: 0,
    name: '',
})

const edit = (scope: anyObj) => {
    baTable.onTableAction('edit', scope)
}

const add = (scope: anyObj) => {
    baTable.form.operate = 'AddProcess'
    baTable.form.items!.id = scope.row.id
    baTable.form.items!.name = scope.row.name
}

const del = (scope: anyObj) => {
    baTable.onTableAction('delete', scope)
}


/**
 * 切换类型
 */
const changeTable = (table: string) => {
    state.table = table;
    baTable.table.filter!.type = table;
    baTable.getIndex();
}


/**
 * 设置状态 
 */
const setStatus = (id: number, status: number, row: anyObj) => {
    let data = {id: id, status: status}
    setStatusApi(data).then((e) => {
        row.status = e.data.status;
        state.sub_status = e.data.sum_status
    })
}


let optButtons = defaultOptButtons([])

/**
 * 查看报价
 * @param row 
 */
const viewQuotation = (row: anyObj) => {
    window.open('/system/business/quotation/' + row.id);
    return;
    baTable.form.operate = 'quotation';
    quotation.id = row.id;
    quotation.name = row.name;
}
/**
 * 关闭
 */
const closeViewQuotation = () => {
    quotation.id = 0;
}

// 自定义一个新的按钮
let newButton: OptButton[] = [
    {
        render: 'basicButton', // 渲染方式:tipButton=带tip的按钮,confirmButton=带确认框的按钮,moveButton=移动按钮
        name: 'edit',
        title: 'edit',
        text: t('编辑'),
        type: 'primary',
        class: 'btn btn-primary',
        icon: '',
    },
    {
        render: 'confirmButton',
        name: 'delete',
        title: '',
        text: t('删除'),
        type: 'danger',
        class: 'btn btn-danger',
        icon: '',
        popconfirm: {
            confirmButtonText: t('删除'),
            cancelButtonText: t('取消'),
            confirmButtonType: 'danger',
            title: t('确定要删除这条数据吗'),
        }
    }
]

// 新按钮合入到默认的按钮数组
optButtons = newButton.concat(optButtons)

// const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])





/**
 * baTable 内包含了表格的所有数据且数据具备响应性，然后通过 provide 注入给了后代组件
 */
const baTable = new baTableClass(
    new baTableApi('/system/corp.Supplier/'),
    {
        pk: 'id',
        column: [
            { type: 'selection', align: 'center', operator: false },
            // { 
            //     label: t('所属分类'), 
            //     prop: 'id', 
            //     align: 'center', 
            //     operatorPlaceholder: t('Fuzzy query'), 
            //     operator: 'LIKE', 
            //     sortable: false,
            //     render: 'slot',
            //     slotName: 'category',
            // },
            // { 
            //     label: t('类型'), 
            //     prop: 'type', 
            //     align: 'center', 
            //     operatorPlaceholder: t('Fuzzy query'), 
            //     operator: 'LIKE', 
            //     sortable: false,
            //     render: 'string', 
            //     renderFormatter: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
            //         if(value){
            //             let arr = value.split(',');
            //             let str = '';
            //             arr.forEach(v => {
            //                 if(str == ''){
            //                     str += t(v);
            //                 }else{
            //                     str += ',' + t(v);
            //                 }
            //             })
            //             return str;
            //         }else{
            //             return '';
            //         }
                    
            //     }
            // },
            { 
                label: t('公司名'), 
                prop: 'corp_name_cn', 
                align: 'center', 
                operatorPlaceholder: t('Fuzzy query'), 
                operator: 'LIKE', 
                sortable: false,
                render: 'slot',
                slotName: 'corp_name_cn'
            },
            { label: t('负责人'), prop: 'manager', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('联系方式'), prop: 'tel', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { 
            //     label: t('地区'), prop: 'tel', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false,
            //     render: 'string', 
            //     renderFormatter: (row: TableRow, field: TableColumn, value: any, column: TableColumnCtx<TableRow>, index: number) => {
            //         let str = "";
            //         if(row.province) str += row.province + '/';
            //         if(row.city) str += row.city + '/';
            //         if(row.area) str += row.area;
            //         return str;
            //     }
            // },
            { label: t('详细地址'), prop: 'address', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            // { label: t('法人'), prop: 'legal_person', align: 'center', operatorPlaceholder: t('Fuzzy query'), operator: 'LIKE', sortable: false },
            { label: t('添加时间'), prop: 'create_time', align: 'center', render: 'datetime', timeFormat: 'yy-mm-dd', width: 100 },
            { label: t('操作'), align: 'center', render: 'buttons', buttons: optButtons, operator: false, width: 150 },
        ],
        dblClickNotEditColumn: ['all'],
        defaultOrder: { prop: 'id', order: 'desc' },
    },
    {
        defaultItems: {  },
    },{
        
    },{
        getIndex: ({ res }: { res: ApiResponse }) => { // 查看后
            state.sub_status = res.data.sum_status;
            // console.log(res.data);
            // console.log(state.sub_status)
        },
        requestEdit: ({ res }: {res: ApiResponse}) => { // 编辑请求后
            // console.log(res)
            state.industry.old = res.data.industry;
            
            setTimeout(function(){
                state.pop = true;
            }, 100)
            
            // baTable.form.items!.industry = res.data.industry
        }
    }
)

provide('baTable', baTable)

onMounted(() => {
    baTable.table.ref = tableRef.value
    baTable.mount()
    baTable.getIndex()?.then(() => {
        baTable.initSort()
        baTable.dragSort()
    })
    getCategoryOldApi().then((e) => {
        console.log(e)
        search.industry.old = e.data;
    })

})
</script>

<style scoped lang="scss">

:deep(.el-image){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    // max-width: 80px;
    // max-height: 80px;
}
:deep(td.business-name > div.cell) {
    min-height: 80px;
    display:flex;
    justify-content:center; 
    align-items:center;
}
.tian{
    cursor: pointer;
}
.tian:hover{
    color: #409EFF;
}

:deep(.el-image > img){
    // width: 60px;
    // height: 60px;
    width: unset;
    height: unset;
    // max-height: 100px;
    max-width: 80px;
    max-height: 80px;
}
:deep(.ba-data-table .btn){
    padding: 4px 14px;
}


:deep(.ba-data-table .margin-left-none:last-child){
    margin-bottom: 0px;
}
:deep(.table-operate-text){
    padding-left: 0;
}
:deep(.font-size-13){
    font-size: 13px;
}
:deep(.pay-type .el-radio){
    margin-right: 10px;
}
pre{
    font-family: 'Helvetica Neue',Helvetica,'PingFang SC','Hiragino Sans GB','Microsoft YaHei','微软雅黑',Arial,sans-serif;
}
.corporate-name:hover{
    color: #409eff!important;
}
:deep(.ba-operate-dialog .el-dialog__body){
    // padding-bottom: 0;
    padding-left: 0;
    padding-right: 0;
}
:deep(.quotation-scrollbar .el-scrollbar:first-child > .el-scrollbar__wrap){
    height: 100%;
}
:deep(.ml-0){
    margin-left: 0!important;
}
:deep(.mt-8){
    margin-top: 8px;
}
:deep(.ml--6){
    margin-left: -6px;
}
:deep(.ml-5){
    margin-left: 5px;
}
:deep(.mt-5){
    margin-top: 5px;
}
:deep(.ba-operate-dialog .el-dialog__body){
    // height: calc(90vh - 55px);
}
</style>
